import { devDebug } from '../../utils/settings';

Page({
  data: {
    appointmentId: '',
    stadiumId: '',
    disabled: 1,  // 是否可选
    appointment: null,
    // 用户查看
    payable: 0,
    sportId: -1,
    dateIdx: -1,
    dateList: [],
    appointmentValid: [],
    orderPrice: 0,
  },
  onLoad(options) {
    const stadiumId = options.appointmentId.substring(0, options.appointmentId.lastIndexOf('-'));
    this.setData({
      appointmentId: options.appointmentId,
      stadiumId,
      disabled: typeof (options.disabled) !== 'undefined' ? parseInt(options.disabled) : 1,
      payable: parseInt(options.payable) || 0,
      sportId: typeof (options.sportId) !== 'undefined' ? parseInt(options.sportId) : -1,
    })
    const eventChannel = this.getOpenerEventChannel()
    eventChannel.on('userView', (data) => {
      this.setData(data);
    })
    this.loadAppointmentData();
  },
  onShow() {
    this.loadAppointmentData()
  },
  async loadAppointmentData() {
    wx.showLoading({ title: '加载中' })
    const appointmentId = this.data.appointmentId, sportId = this.data.sportId;
    let resList = await new Promise(resolve => {
      wx.cloud.callFunction({
        name: 'db_appointment',
        data: { funcName: 'queryOne', _id: appointmentId, sport_id: sportId },
        success: (r) => resolve(r.result),
      })
    });
    if (resList.length > 0 && resList[0].courts.length > 0) {
      console.log(resList[0]);
      this.setData({ appointment: resList[0] })
    } else {
      wx.showToast({ title: '当天没有此类场地的预约', icon: 'none' })
      this.setData({ appointment: null })
    }
    this.setData({ orderPrice: 0 })
    wx.hideLoading()
  },
  createPayment() {
    if (this.data.orderPrice) {
      const res = this.selectComponent('#tb_appointment');
      const self = this;
      wx.navigateTo({
        url: '/pages/creator/order',
        success: (r) => {
          r.eventChannel.emit('init', {
            appointmentId: self.data.appointmentId,
            stadiumName: self.data.stadiumName,
            sportId: self.data.sportId,
            dateInfo: self.data.dateList[self.data.dateIdx],
            orderPrice: self.data.orderPrice,
            courtList: res.courtList,
            skuList: res.skuList,
          })
        }
      })
    } else {
      wx.showToast({
        title: '请选择场次',
        icon: 'none'
      })
    }

  },
  async onActivateDate(ev) {
    const idx = ev.currentTarget.dataset.idx;
    if (this.data.appointmentValid[idx] == 0) return;
    let confirm = (this.data.orderPrice == 0);
    if (!confirm) {
      confirm = await new Promise(resolve => {
        wx.showModal({
          title: '提示',
          content: '确定更换预约日期',
          success: (r) => resolve(r.confirm)
        })
      })
    }
    if (!confirm) return;
    this.setData({
      dateIdx: idx,
      appointmentId: this.data.stadiumId + '-' + parseInt(new Date(this.data.dateList[idx].dateStr).getTime() / 1e5)
    });
    this.loadAppointmentData();
  },
  onChangePrice(ev) {
    this.setData({ orderPrice: ev.detail.price });
  }
})